<script setup lang="jsx">
  import {watch,inject,ref,reactive,provide} from 'vue'
  import workOrder from '@/api/logistics/workOrder'
  import WorkInfo from '@/views/logistics/workOrder/info.vue'
  import dictHelp from "@/utils/dicthelp.js";
  const crudRef = ref()
  const record = inject('record')
  const workOrderData = inject('workOrderData')
  const workInfo = ref()
  const getRecord = inject('getRecord',()=>{})
  const dictList = inject('dictList') //全部字典
  const dicthelpApi_user = inject('dicthelpApi_user')//人員
  watch(record,(newVal,oldVal)=>{
    if(newVal.id != undefined){
      console.log('刷新工单列表--'+record.value.id)
      crudRef.value.requestData()
    }
  })
  let options = reactive({
      pk: 'id',
      pageSize:100,
      autoRequest: record.value?.id > 0,
      showTools:false,
      expandSearch:false,
      formOption: {
        viewType: 'drawer',
        width: "70%",
      },
      // 右键菜单配置
      contextMenu: {
        // 是否开启右键菜单
        enabled: false,
      },
      isDbClickEdit:false,
      // operationColumn: true,
      // operationColumnWidth: 80,
      tablePagination:{style:{display:"none"}},
      // api: workOrder.getList,
      api:'',
      beforeRequest:(param)=>{
        param.about_id = record.value.id
        return param;
      },
      beforeAdd:(param)=>{
        param.about_id = record.value.id
        return param;
      },
      afterRequest:(res)=>{       
        return record.value.work_orders
      },
      add: {
        show: false,
        api: workOrder.save,
        auth: ['logistics:workOrder:save'],
      },
      edit: {
        show: true,
        api: workOrder.update,
        auth: ['logistics:workOrder:update'],
        action: (record)=>{
          console.log(record)
          workInfo.value.open(record)
        },
      },
      afterEdit:()=>{
        workInfo.value.refreshRecode()
        getRecord()
      },
      afterAdd:()=>{
        getRecord()
      },
    })

const columns = ref([
  {title: '单据类型',hide: true,
  disabled:true,
  dataIndex: "about_type",
  addDefaultValue: workOrderData.value.type,
    dict: {
      // name: "tagType",
      data:[],
      props: {
        label: "title",
        value: "key"
      },
      translation: true
      },
    formType: "select",
    commonRules: {
      required: true,
      message: "请选择单据类型"
    }
  },
  {
    title: "关注人",
    dataIndex: "admin",
    formType: "select",
    virtualListProps:{},
    dict: {
        data:dicthelpApi_user,
        props: {label: 'username', value: 'id'}, translation: true,
    },
    multiple:true,
    addDisplay:false,
    // editDisplay:false,
    hide: true
  },
  {
    title: "创建人",
    dataIndex: "created_by",
    formType: "select",
    virtualListProps:{},
    dict: {
        data:dicthelpApi_user,
        props: {label: 'username', value: 'id'}, translation: true,
    },
    hide:true,
    addDisplay: false,
    editDisplay: false,
  },
  {title: '标题', dataIndex: 'title', formType: 'input',
    commonRules: {
      required: true,
      message: "请填写标题"
    }
  },
  {title: '描述', dataIndex: 'remark', formType: 'textarea',
    commonRules: {
      required: true,
      message: "请填写描述"
    }
  },
  {
      title: "",
      dataIndex: "user_visible",
      formType: "checkbox",
      hide:true,
      dict:{
        data:[
          {label:'用户可见',value:1},
        ]
      },
    },
    {
      title: "",
      dataIndex: "intercept",
      formType: "checkbox",
      hide:true,
      dict:{
        data:[
          {label:'拦截',value:1},
        ]
      },
    },
    {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    showTime: true
  },
  {
    title: "状态",
    dataIndex: "status",
    formType: "select",
    addDisplay:false,
    editDisplay:false,
    dict: {
      // name: "workOrderStatus",
      data:[],
      props: {
        label: "title",
        value: "key"
      },
      translation: true
      },
  },
]);
//字典赋值
columns.value.forEach(function(item) {
  if(item.dataIndex=='about_type'){
    item.dict.data=dictList.value.tagType
  }
  if(item.dataIndex=='status'){
    item.dict.data=dictList.value.workOrderStatus
  } 
  })
provide('WorkOrder',crudRef)
</script>

<template>
  <a-grid-item :span="workOrderData.span">
    <a-collapse :default-active-key="['1']" class="g-collapse-no-p">
      <a-collapse-item header="工单" key="1">
        <template #extra>
          <a-button-group>
            <a-button type="primary" size="mini" @click.stop="crudRef.addAction()" v-auth="['logistics:workOrder:save']">创建</a-button>
          </a-button-group>
        </template>
          <ma-crud :options="options" :columns="columns" ref="crudRef" class="file-list">
            <template #title="{ record }">
              <a-link color="blue" @click="crudRef.editAction(record)">{{ record.title }}</a-link>
            </template>
          </ma-crud>
          <work-info ref="workInfo" />
      </a-collapse-item>
    </a-collapse>
  </a-grid-item>
</template>
<style>
.file-list ._crud-header,.file-list .mb-2,.file-list .operation-tools,.file-list .mb-1{
  display:none;
}

</style>